package com.siyoumi.modules.admin.web;

import com.siyoumi.controller.AdminApiController;
import com.siyoumi.entity.*;
import com.siyoumi.modules.account.service.SvcAccount;
import com.siyoumi.modules.account.vo.VoChangeStore;
import com.siyoumi.modules.admin.service.AdminService;
import com.siyoumi.modules.admin.vo.ApprVo;
import com.siyoumi.util.XReturn;
import com.siyoumi.validator.XValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/xadmin/sys/center")
public class ApiAdminCenter
        extends AdminApiController {
    @GetMapping()
    public XReturn center() {
        SysAccountService appAcc = SysAccountService.getBean();
        SysAccount entityAcc = appAcc.getSysAccountByToken();

        //应用ID
        List<SysApp> apps = SysAppService.getBean().getApps(entityAcc, true);
        List<String> appIds = SysAppService.getBean().getAppIds(entityAcc, true);

        List<SysAppRouter> listAppr = appAcc.getListAppRouter(entityAcc);
        List<ApprVo> listApprVo = AdminService.getBean().appRouterToApprVo(listAppr);


        Boolean existsWebsite = apps.stream().findFirst().map(app ->
        {
            return app.getApp_id().equals("website");
        }).get();

        XReturn r = XReturn.getR(0);
        r.setData("acc", entityAcc);
        r.setData("app_ids", appIds);
        r.setData("apps_r", listApprVo);
        r.setData("x_center", SysAccsuperConfigService.isCenter());
        //商家信息，非商家类型帐号，返回null
        Map<String, Object> mapStore = SvcAccount.getBean().storeInfo(entityAcc);
        r.setData("store", mapStore);

        return r;
    }

    //切换商家
    @PostMapping("change_store")
    public XReturn changeStore(@Validated VoChangeStore vo, BindingResult result) {
        //通用验证
        XValidator.getResult(result, true, true);
        //
        vo.setAcc_id(getAccId());
        //
        return SvcAccount.getBean().changeStore(vo);
    }
}
